package com.cqndt.site.service.back.impl;

import com.cqndt.site.dao.SysUserMapper;
import com.cqndt.site.service.back.SysUserService;
import com.cqndt.site.util.EncodePasswordUtil;
import com.cqndt.site.util.Result;
import com.cqndt.site.vo.SysUserVo;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper userMapper;

    @Override
    public List<SysUserVo> listByCondition(SysUserVo user) {
        return userMapper.listByCondition(user);
    }

    @Override
    public Result addUser(SysUserVo vo) {
        Result result = new Result();
        if(null == vo.getUserName()){
            return result.failure(-1,"用户名不能为空");
        }
        SysUserVo user = userMapper.getByUserName(vo.getUserName());
        if(null != user){
            return result.failure(-1,"用户名已存在");
        }
        String salt = RandomStringUtils.randomAlphanumeric(20);
        vo.setSalt(salt);
        //密码加密
        vo.setPassword(EncodePasswordUtil.sha256(vo.getPassword(),salt));
        vo.setStatus(1);
        vo.setCreateTime(new Date());
        userMapper.addUser(vo);
        result.setData(vo);
        return result;
    }

    @Override
    public Result updateUser(SysUserVo vo) {
        Result result = new Result();
        if(null == vo.getUserName()){
            return result.failure(-1,"用户名不能为空");
        }
        SysUserVo user = userMapper.getByUserName(vo.getUserName());
        if(null != user && vo.getId() != user.getId()){
            return result.failure(-1,"用户名已存在");
        }
        vo.setUpTime(new Date());
        userMapper.updateUser(vo);
        return result;
    }

    @Override
    public Result deleteUser(String ids) {
        Result result = new Result();
        if(null != ids){
            userMapper.deleteUser(ids.split(","));
        }
        return result.success("删除成功");
    }

    @Override
    public Result upPassword(Integer userId, String oldPass, String newPass) {
        Result result = new Result();
        SysUserVo vo = userMapper.getById(userId);
        String oldChPass = EncodePasswordUtil.sha256(oldPass, vo.getSalt());
        if(!oldChPass.equals(vo.getPassword())){
            return result.failure(-1,"原密码输入错误");
        }
        vo.setPassword(EncodePasswordUtil.sha256(newPass, vo.getSalt()));
        userMapper.updateUser(vo);
        return result.success("修改成功");
    }

    @Override
    public Result resetPass(Integer userId, String newPass) {
        Result result = new Result();
        if(null == userId){
            return result.failure(-1,"重置密码对象id为空");
        }
        SysUserVo vo =  userMapper.getById(userId);
        if(null == vo){
            return result.failure(-1,"重置密码对象为空");
        }
        String pass = EncodePasswordUtil.sha256(newPass, vo.getSalt());
        vo.setPassword(pass);
        userMapper.updateUser(vo);
        return result.success("重置成功");
    }
}
